package cn.iocoder.yudao.module.polling.service.pollingcategoriesrelevance;

import cn.iocoder.yudao.module.polling.controller.admin.pollingcategoriesrelevance.vo.PollingCategoriesRelevanceCreateReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingcategoriesrelevance.vo.PollingCategoriesRelevanceExportReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingcategoriesrelevance.vo.PollingCategoriesRelevancePageReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingcategoriesrelevance.vo.PollingCategoriesRelevanceUpdateReqVO;
import cn.iocoder.yudao.module.polling.convert.pollingcategoriesrelevance.PollingCategoriesRelevanceConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingcategoriesrelevance.PollingCategoriesRelevanceDO;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingcategoriesrelevance.PollingCategoriesRelevanceMapper;
import cn.iocoder.yudao.module.polling.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 巡检标准适用类别关联 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class PollingCategoriesRelevanceServiceImpl implements PollingCategoriesRelevanceService {

    @Resource
    private PollingCategoriesRelevanceMapper pollingCategoriesRelevanceMapper;

    @Override
    public Integer createPollingCategoriesRelevance(PollingCategoriesRelevanceCreateReqVO createReqVO) {
        // 插入
        PollingCategoriesRelevanceDO pollingCategoriesRelevance = PollingCategoriesRelevanceConvert.INSTANCE.convert(createReqVO);
        pollingCategoriesRelevanceMapper.insert(pollingCategoriesRelevance);
        // 返回
        return pollingCategoriesRelevance.getId();
    }

    @Override
    public void updatePollingCategoriesRelevance(PollingCategoriesRelevanceUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePollingCategoriesRelevanceExists(updateReqVO.getId());
        // 更新
        PollingCategoriesRelevanceDO updateObj = PollingCategoriesRelevanceConvert.INSTANCE.convert(updateReqVO);
        pollingCategoriesRelevanceMapper.updateById(updateObj);
    }

    @Override
    public void deletePollingCategoriesRelevance(Integer id) {
        // 校验存在
        this.validatePollingCategoriesRelevanceExists(id);
        // 删除
        pollingCategoriesRelevanceMapper.deleteById(id);
    }

    private void validatePollingCategoriesRelevanceExists(Integer id) {
        if (pollingCategoriesRelevanceMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public PollingCategoriesRelevanceDO getPollingCategoriesRelevance(Integer id) {
        return pollingCategoriesRelevanceMapper.selectById(id);
    }

    @Override
    public List<PollingCategoriesRelevanceDO> getPollingCategoriesRelevanceList(Collection<Integer> ids) {
        return pollingCategoriesRelevanceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PollingCategoriesRelevanceDO> getPollingCategoriesRelevancePage(PollingCategoriesRelevancePageReqVO pageReqVO) {
        return pollingCategoriesRelevanceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PollingCategoriesRelevanceDO> getPollingCategoriesRelevanceList(PollingCategoriesRelevanceExportReqVO exportReqVO) {
        return pollingCategoriesRelevanceMapper.selectList(exportReqVO);
    }

}
